const mime = require("mime");
const zlib = require('zlib');
const readable = require('stream').Readable;

/**
 * resWrite
 * @param {object} args
 * @param {Function} callback
 */
function resWrite(args, callback) {
  let response = args.response;
  let statusCode = args.statusCode || 200;
  let mimeKey = args.mimeKey;
  let dataType = args.dataType || 'string';
  let data = args.data || null;
  let stream = args.stream || null;
  let encoding = args.encoding || null;
  let cookies = args.cookies || null;
  let cache = args.cache || false;
  let lastModified = args.lastModified || null;
  let mimeType = mime.getType(mimeKey);
  new Promise((resolve, reject) => {
    response ? resolve() : reject();
  }).then(
      () => {
        new Promise((resolve, reject) => {
          dataType === 'stream' ? resolve() : reject();
        }).then(
            //dataType is stream
            () => {
              new Promise((resolve, reject) => {
                stream ? resolve() : reject();
              }).then(
                  //data is stream
                  () => {
                    new Promise((resolve, reject) => {
                      cache ? resolve() : reject();
                    }).then(
                        //use cache
                        () => {
                          new Promise((resolve, reject) => {
                            lastModified ? resolve() : reject();
                          }).then(
                              //have lastModified
                              () => {
                                new Promise((resolve, reject) => {
                                  encoding ? resolve() : reject();
                                }).then(
                                    //encoding support
                                    () => {
                                      new Promise((resolve, reject) => {
                                        encoding.match(/gzip/) ? resolve() : reject();
                                      }).then(
                                          //gzip support
                                          () => {
                                            let compress = zlib.createGzip();
                                            let compressType = 'gzip';
                                            new Promise((resolve, reject) => {
                                              cookies ? resolve() : reject();
                                            }).then(
                                                //use cookie
                                                () => {
                                                  new Promise((resolve, reject) => {
                                                    try {
                                                      response.writeHead(statusCode, {
                                                        "Content-Type": mimeType + ';charset=utf-8',
                                                        "Cache-Control": "public, max-age=0",
                                                        "Set-Cookie": cookies,
                                                        "Last-Modified": lastModified,
                                                        'Content-Encoding': compressType,
                                                        "server": `node/${process.version}`,
                                                        "service": process.pid
                                                      });
                                                      resolve();
                                                    } catch (error) {
                                                      reject(error);
                                                    }
                                                  }).then(
                                                      () => {
                                                        stream.pipe(compress).pipe(response);
                                                        callback(null, statusCode);
                                                      }
                                                      , (error) => {
                                                        stream.pipe(response);
                                                        callback(error, statusCode);
                                                      }
                                                  ).catch((error) => {
                                                    callback(error, null);
                                                  })
                                                }
                                                //not use cookie
                                                , () => {
                                                  new Promise((resolve, reject) => {
                                                    try {
                                                      response.writeHead(statusCode, {
                                                        "Content-Type": mimeType + ';charset=utf-8',
                                                        "Cache-Control": "public, max-age=0",
                                                        "Last-Modified": lastModified,
                                                        'Content-Encoding': compressType,
                                                        "server": `node/${process.version}`,
                                                        "service": process.pid
                                                      });
                                                      resolve();
                                                    } catch (error) {
                                                      reject(error);
                                                    }
                                                  }).then(
                                                      () => {
                                                        stream.pipe(compress).pipe(response);
                                                        callback(null, statusCode);
                                                      }
                                                      , (error) => {
                                                        stream.pipe(response);
                                                        callback(error, statusCode);
                                                      }
                                                  ).catch((error) => {
                                                    callback(error, null);
                                                  })
                                                }
                                            ).catch((error) => {
                                              callback(error, null);
                                            })
                                          }
                                          //gzip not support
                                          , () => {
                                            new Promise((resolve, reject) => {
                                              encoding.match(/deflate/) ? resolve() : reject();
                                            }).then(
                                                //deflate support
                                                () => {
                                                  let compress = zlib.createDeflate();
                                                  let compressType = 'deflate';
                                                  new Promise((resolve, reject) => {
                                                    cookies ? resolve() : reject();
                                                  }).then(
                                                      //use cookie
                                                      () => {
                                                        new Promise((resolve, reject) => {
                                                          try {
                                                            response.writeHead(statusCode, {
                                                              "Content-Type": mimeType + ';charset=utf-8',
                                                              "Cache-Control": "public, max-age=0",
                                                              "Set-Cookie": cookies,
                                                              "Last-Modified": lastModified,
                                                              'Content-Encoding': compressType,
                                                              "server": `node/${process.version}`,
                                                              "service": process.pid
                                                            });
                                                            resolve();
                                                          } catch (error) {
                                                            reject(error);
                                                          }
                                                        }).then(
                                                            () => {
                                                              stream.pipe(compress).pipe(response);
                                                              callback(null, statusCode);
                                                            }
                                                            , (error) => {
                                                              stream.pipe(response);
                                                              callback(error, statusCode);
                                                            }
                                                        ).catch((error) => {
                                                          callback(error, null);
                                                        })
                                                      }
                                                      //not use cookie
                                                      , () => {
                                                        new Promise((resolve, reject) => {
                                                          try {
                                                            response.writeHead(statusCode, {
                                                              "Content-Type": mimeType + ';charset=utf-8',
                                                              "Cache-Control": "public, max-age=0",
                                                              "Last-Modified": lastModified,
                                                              'Content-Encoding': compressType,
                                                              "server": `node/${process.version}`,
                                                              "service": process.pid
                                                            });
                                                            resolve();
                                                          } catch (error) {
                                                            reject(error);
                                                          }
                                                        }).then(
                                                            () => {
                                                              stream.pipe(compress).pipe(response);
                                                              callback(null, statusCode);
                                                            }
                                                            , (error) => {
                                                              stream.pipe(response);
                                                              callback(error, statusCode);
                                                            }
                                                        ).catch((error) => {
                                                          callback(error, null);
                                                        })
                                                      }
                                                  ).catch((error) => {
                                                    callback(error, null);
                                                  })
                                                }
                                                //encoding not support
                                                , () => {
                                                  new Promise((resolve, reject) => {
                                                    cookies ? resolve() : reject();
                                                  }).then(
                                                      //use cookie
                                                      () => {
                                                        new Promise((resolve, reject) => {
                                                          try {
                                                            response.writeHead(statusCode, {
                                                              "Content-Type": mimeType + ';charset=utf-8',
                                                              "Cache-Control": "public, max-age=0",
                                                              "Set-Cookie": cookies,
                                                              "Last-Modified": lastModified,
                                                              "server": `node/${process.version}`,
                                                              "service": process.pid
                                                            });
                                                            resolve();
                                                          } catch (error) {
                                                            reject(error);
                                                          }
                                                        }).then(
                                                            () => {
                                                              stream.pipe(response);
                                                              callback(null, statusCode);
                                                            }
                                                            , (error) => {
                                                              stream.pipe(response);
                                                              callback(error, statusCode);
                                                            }
                                                        ).catch((error) => {
                                                          callback(error, null);
                                                        })
                                                      }
                                                      //not use cookie
                                                      , () => {
                                                        new Promise((resolve, reject) => {
                                                          try {
                                                            response.writeHead(statusCode, {
                                                              "Content-Type": mimeType + ';charset=utf-8',
                                                              "Cache-Control": "public, max-age=0",
                                                              "Last-Modified": lastModified,
                                                              "server": `node/${process.version}`,
                                                              "service": process.pid
                                                            });
                                                            resolve();
                                                          } catch (error) {
                                                            reject(error);
                                                          }
                                                        }).then(
                                                            () => {
                                                              stream.pipe(response);
                                                              callback(null, statusCode);
                                                            }
                                                            , (error) => {
                                                              stream.pipe(response);
                                                              callback(error, statusCode);
                                                            }
                                                        ).catch((error) => {
                                                          callback(error, null);
                                                        })
                                                      }
                                                  ).catch((error) => {
                                                    callback(error, null);
                                                  })
                                                }
                                            ).catch((error) => {
                                              callback(error, null);
                                            })
                                          }
                                      ).catch((error) => {
                                        callback(error, null);
                                      })
                                    }
                                    //encoding not support
                                    , () => {
                                      new Promise((resolve, reject) => {
                                        cookies ? resolve() : reject();
                                      }).then(
                                          //use cookie
                                          () => {
                                            new Promise((resolve, reject) => {
                                              try {
                                                response.writeHead(statusCode, {
                                                  "Content-Type": mimeType + ';charset=utf-8',
                                                  "Cache-Control": "public, max-age=0",
                                                  "Set-Cookie": cookies,
                                                  "Last-Modified": lastModified,
                                                  "server": `node/${process.version}`,
                                                  "service": process.pid
                                                });
                                                resolve();
                                              } catch (error) {
                                                reject(error);
                                              }
                                            }).then(
                                                () => {
                                                  stream.pipe(response);
                                                  callback(null, statusCode);
                                                }
                                                , (error) => {
                                                  stream.pipe(response);
                                                  callback(error, statusCode);
                                                }
                                            ).catch((error) => {
                                              callback(error, null);
                                            })
                                          }
                                          //not use cookie
                                          , () => {
                                            new Promise((resolve, reject) => {
                                              try {
                                                response.writeHead(statusCode, {
                                                  "Content-Type": mimeType + ';charset=utf-8',
                                                  "Cache-Control": "public, max-age=0",
                                                  "Last-Modified": lastModified,
                                                  "server": `node/${process.version}`,
                                                  "service": process.pid
                                                });
                                                resolve();
                                              } catch (error) {
                                                reject(error);
                                              }
                                            }).then(
                                                () => {
                                                  stream.pipe(response);
                                                  callback(null, statusCode);
                                                }
                                                , (error) => {
                                                  stream.pipe(response);
                                                  callback(error, statusCode);
                                                }
                                            ).catch((error) => {
                                              callback(error, null);
                                            })
                                          }
                                      ).catch((error) => {
                                        callback(error, null);
                                      })
                                    }
                                ).catch((error) => {
                                  callback(error, null);
                                })
                              }
                              //have no LastModified
                              , () => {
                                callback(new SyntaxError(`LastModified can't be undefined`), null);
                              }
                          ).catch((error) => {
                            callback(error, null);
                          })
                        }
                        //not use cache
                        , () => {
                          new Promise((resolve, reject) => {
                            encoding ? resolve() : reject();
                          }).then(
                              //encoding support
                              () => {
                                new Promise((resolve, reject) => {
                                  encoding.match(/gzip/) ? resolve() : reject();
                                }).then(
                                    //gzip support
                                    () => {
                                      let compress = zlib.createGzip();
                                      let compressType = 'gzip';
                                      new Promise((resolve, reject) => {
                                        cookies ? resolve() : reject();
                                      }).then(
                                          //use cookie
                                          () => {
                                            new Promise((resolve, reject) => {
                                              try {
                                                response.writeHead(statusCode, {
                                                  "Content-Type": mimeType + ';charset=utf-8',
                                                  "Cache-Control": "public, max-age=0",
                                                  "Set-Cookie": cookies,
                                                  'Content-Encoding': compressType,
                                                  "server": `node/${process.version}`,
                                                  "service": process.pid
                                                });
                                                resolve();
                                              } catch (error) {
                                                reject(error);
                                              }
                                            }).then(
                                                () => {
                                                  stream.pipe(compress).pipe(response);
                                                  callback(null, statusCode);
                                                }
                                                , (error) => {
                                                  stream.pipe(response);
                                                  callback(error, statusCode);
                                                }
                                            ).catch((error) => {
                                              callback(error, null);
                                            })
                                          }
                                          //not use cookie
                                          , () => {
                                            new Promise((resolve, reject) => {
                                              try {
                                                response.writeHead(statusCode, {
                                                  "Content-Type": mimeType + ';charset=utf-8',
                                                  "Cache-Control": "public, max-age=0",
                                                  'Content-Encoding': compressType,
                                                  "server": `node/${process.version}`,
                                                  "service": process.pid
                                                });
                                                resolve();
                                              } catch (error) {
                                                reject(error);
                                              }
                                            }).then(
                                                () => {
                                                  stream.pipe(compress).pipe(response);
                                                  callback(null, statusCode);
                                                }
                                                , (error) => {
                                                  stream.pipe(response);
                                                  callback(error, statusCode);
                                                }
                                            ).catch((error) => {
                                              callback(error, null);
                                            })
                                          }
                                      ).catch((error) => {
                                        callback(error, null);
                                      })
                                    }
                                    //gzip not support
                                    , () => {
                                      new Promise((resolve, reject) => {
                                        encoding.match(/deflate/) ? resolve() : reject();
                                      }).then(
                                          //deflate support
                                          () => {
                                            let compress = zlib.createDeflate();
                                            let compressType = 'deflate';
                                            new Promise((resolve, reject) => {
                                              cookies ? resolve() : reject();
                                            }).then(
                                                //use cookie
                                                () => {
                                                  new Promise((resolve, reject) => {
                                                    try {
                                                      response.writeHead(statusCode, {
                                                        "Content-Type": mimeType + ';charset=utf-8',
                                                        "Cache-Control": "public, max-age=0",
                                                        "Set-Cookie": cookies,
                                                        'Content-Encoding': compressType,
                                                        "server": `node/${process.version}`,
                                                        "service": process.pid
                                                      });
                                                      resolve();
                                                    } catch (error) {
                                                      reject(error);
                                                    }
                                                  }).then(
                                                      () => {
                                                        stream.pipe(compress).pipe(response);
                                                        callback(null, statusCode);
                                                      }
                                                      , (error) => {
                                                        stream.pipe(response);
                                                        callback(error, statusCode);
                                                      }
                                                  ).catch((error) => {
                                                    callback(error, null);
                                                  })
                                                }
                                                //not use cookie
                                                , () => {
                                                  new Promise((resolve, reject) => {
                                                    try {
                                                      response.writeHead(statusCode, {
                                                        "Content-Type": mimeType + ';charset=utf-8',
                                                        "Cache-Control": "public, max-age=0",
                                                        'Content-Encoding': compressType,
                                                        "server": `node/${process.version}`,
                                                        "service": process.pid
                                                      });
                                                      resolve();
                                                    } catch (error) {
                                                      reject(error);
                                                    }
                                                  }).then(
                                                      () => {
                                                        stream.pipe(compress).pipe(response);
                                                        callback(null, statusCode);
                                                      }
                                                      , (error) => {
                                                        stream.pipe(response);
                                                        callback(error, statusCode);
                                                      }
                                                  ).catch((error) => {
                                                    callback(error, null);
                                                  })
                                                }
                                            ).catch((error) => {
                                              callback(error, null);
                                            })
                                          }
                                          //encoding not support
                                          , () => {
                                            new Promise((resolve, reject) => {
                                              cookies ? resolve() : reject();
                                            }).then(
                                                //use cookie
                                                () => {
                                                  new Promise((resolve, reject) => {
                                                    try {
                                                      response.writeHead(statusCode, {
                                                        "Content-Type": mimeType + ';charset=utf-8',
                                                        "Cache-Control": "public, max-age=0",
                                                        "Set-Cookie": cookies,
                                                        "server": `node/${process.version}`,
                                                        "service": process.pid
                                                      });
                                                      resolve();
                                                    } catch (error) {
                                                      reject(error);
                                                    }
                                                  }).then(
                                                      () => {
                                                        stream.pipe(response);
                                                        callback(null, statusCode);
                                                      }
                                                      , (error) => {
                                                        stream.pipe(response);
                                                        callback(error, statusCode);
                                                      }
                                                  ).catch((error) => {
                                                    callback(error, null);
                                                  })
                                                }
                                                //not use cookie
                                                , () => {
                                                  new Promise((resolve, reject) => {
                                                    try {
                                                      response.writeHead(statusCode, {
                                                        "Content-Type": mimeType + ';charset=utf-8',
                                                        "Cache-Control": "public, max-age=0",
                                                        "server": `node/${process.version}`,
                                                        "service": process.pid
                                                      });
                                                      resolve();
                                                    } catch (error) {
                                                      reject(error);
                                                    }
                                                  }).then(
                                                      () => {
                                                        stream.pipe(response);
                                                        callback(null, statusCode);
                                                      }
                                                      , (error) => {
                                                        stream.pipe(response);
                                                        callback(error, statusCode);
                                                      }
                                                  ).catch((error) => {
                                                    callback(error, null);
                                                  })
                                                }
                                            ).catch((error) => {
                                              callback(error, null);
                                            })
                                          }
                                      ).catch((error) => {
                                        callback(error, null);
                                      })
                                    }
                                ).catch((error) => {
                                  callback(error, null);
                                })
                              }
                              //encoding not support
                              , () => {
                                new Promise((resolve, reject) => {
                                  cookies ? resolve() : reject();
                                }).then(
                                    //use cookie
                                    () => {
                                      new Promise((resolve, reject) => {
                                        try {
                                          response.writeHead(statusCode, {
                                            "Content-Type": mimeType + ';charset=utf-8',
                                            "Cache-Control": "public, max-age=0",
                                            "Set-Cookie": cookies,
                                            "server": `node/${process.version}`,
                                            "service": process.pid
                                          });
                                          resolve();
                                        } catch (error) {
                                          reject(error);
                                        }
                                      }).then(
                                          () => {
                                            stream.pipe(response);
                                            callback(null, statusCode);
                                          }
                                          , (error) => {
                                            stream.pipe(response);
                                            callback(error, statusCode);
                                          }
                                      ).catch((error) => {
                                        callback(error, null);
                                      })
                                    }
                                    //not use cookie
                                    , () => {
                                      new Promise((resolve, reject) => {
                                        try {
                                          response.writeHead(statusCode, {
                                            "Content-Type": mimeType + ';charset=utf-8',
                                            "Cache-Control": "public, max-age=0",
                                            "server": `node/${process.version}`,
                                            "service": process.pid
                                          });
                                          resolve();
                                        } catch (error) {
                                          reject(error);
                                        }
                                      }).then(
                                          () => {
                                            stream.pipe(response);
                                            callback(null, statusCode);
                                          }
                                          , (error) => {
                                            stream.pipe(response);
                                            callback(error, statusCode);
                                          }
                                      ).catch((error) => {
                                        callback(error, null);
                                      })
                                    }
                                ).catch((error) => {
                                  callback(error, null);
                                })
                              }
                          ).catch((error) => {
                            callback(error, null);
                          })
                        }
                    ).catch((error) => {
                      callback(error, null);
                    })
                  }
                  //data is null
                  , () => {
                    callback(new SyntaxError(`stream can't be undefined`), null);
                  }
              ).catch((error) => {
                callback(error, null);
              })
            }
            //dataType is string
            , () => {
              stream = new readable()
              stream.push(data);
              stream.push(null);
              new Promise((resolve, reject) => {
                cache ? resolve() : reject();
              }).then(
                  //use cache
                  () => {
                    new Promise((resolve, reject) => {
                      lastModified ? resolve() : reject();
                    }).then(
                        //have lastModified
                        () => {
                          new Promise((resolve, reject) => {
                            encoding ? resolve() : reject();
                          }).then(
                              //encoding support
                              () => {
                                new Promise((resolve, reject) => {
                                  encoding.match(/gzip/) ? resolve() : reject();
                                }).then(
                                    //gzip support
                                    () => {
                                      let compress = zlib.createGzip();
                                      let compressType = 'gzip';
                                      new Promise((resolve, reject) => {
                                        cookies ? resolve() : reject();
                                      }).then(
                                          //use cookie
                                          () => {
                                            new Promise((resolve, reject) => {
                                              try {
                                                response.writeHead(statusCode, {
                                                  "Content-Type": mimeType + ';charset=utf-8',
                                                  "Cache-Control": "public, max-age=0",
                                                  "Set-Cookie": cookies,
                                                  "Last-Modified": lastModified,
                                                  'Content-Encoding': compressType,
                                                  "server": `node/${process.version}`,
                                                  "service": process.pid
                                                });
                                                resolve();
                                              } catch (error) {
                                                reject(error);
                                              }
                                            }).then(
                                                () => {
                                                  stream.pipe(compress).pipe(response);
                                                  callback(null, statusCode);
                                                }
                                                , (error) => {
                                                  stream.pipe(response);
                                                  callback(error, statusCode);
                                                }
                                            ).catch((error) => {
                                              callback(error, null);
                                            })
                                          }
                                          //not use cookie
                                          , () => {
                                            new Promise((resolve, reject) => {
                                              try {
                                                response.writeHead(statusCode, {
                                                  "Content-Type": mimeType + ';charset=utf-8',
                                                  "Cache-Control": "public, max-age=0",
                                                  "Last-Modified": lastModified,
                                                  'Content-Encoding': compressType,
                                                  "server": `node/${process.version}`,
                                                  "service": process.pid
                                                });
                                                resolve();
                                              } catch (error) {
                                                reject(error);
                                              }
                                            }).then(
                                                () => {
                                                  stream.pipe(compress).pipe(response);
                                                  callback(null, statusCode);
                                                }
                                                , (error) => {
                                                  stream.pipe(response);
                                                  callback(null, statusCode);
                                                }
                                            ).catch((error) => {
                                              callback(error, null);
                                            })
                                          }
                                      ).catch((error) => {
                                        callback(error, null);
                                      })
                                    }
                                    //gzip not support
                                    , () => {
                                      new Promise((resolve, reject) => {
                                        encoding.match(/deflate/) ? resolve() : reject();
                                      }).then(
                                          //deflate support
                                          () => {
                                            let compress = zlib.createDeflate();
                                            let compressType = 'deflate';
                                            new Promise((resolve, reject) => {
                                              cookies ? resolve() : reject();
                                            }).then(
                                                //use cookie
                                                () => {
                                                  new Promise((resolve, reject) => {
                                                    try {
                                                      response.writeHead(statusCode, {
                                                        "Content-Type": mimeType + ';charset=utf-8',
                                                        "Cache-Control": "public, max-age=0",
                                                        "Set-Cookie": cookies,
                                                        "Last-Modified": lastModified,
                                                        'Content-Encoding': compressType,
                                                        "server": `node/${process.version}`,
                                                        "service": process.pid
                                                      });
                                                      resolve();
                                                    } catch (error) {
                                                      reject(error);
                                                    }
                                                  }).then(
                                                      () => {
                                                        stream.pipe(compress).pipe(response);
                                                        callback(null, statusCode);
                                                      }
                                                      , (error) => {
                                                        stream.pipe(response);
                                                        callback(null, statusCode);
                                                      }
                                                  ).catch((error) => {
                                                    callback(error, null);
                                                  })
                                                }
                                                //not use cookie
                                                , () => {
                                                  new Promise((resolve, reject) => {
                                                    try {
                                                      response.writeHead(statusCode, {
                                                        "Content-Type": mimeType + ';charset=utf-8',
                                                        "Cache-Control": "public, max-age=0",
                                                        "Last-Modified": lastModified,
                                                        'Content-Encoding': compressType,
                                                        "server": `node/${process.version}`,
                                                        "service": process.pid
                                                      });
                                                      resolve();
                                                    } catch (error) {
                                                      reject(error);
                                                    }
                                                  }).then(
                                                      () => {
                                                        stream.pipe(compress).pipe(response);
                                                        callback(null, statusCode);
                                                      }
                                                      , (error) => {
                                                        stream.pipe(response);
                                                        callback(null, statusCode);
                                                      }
                                                  ).catch((error) => {
                                                    callback(error, null);
                                                  })
                                                }
                                            ).catch((error) => {
                                              callback(error, null);
                                            })
                                          }
                                          //encoding not support
                                          , () => {
                                            new Promise((resolve, reject) => {
                                              cookies ? resolve() : reject();
                                            }).then(
                                                //use cookie
                                                () => {
                                                  new Promise((resolve, reject) => {
                                                    try {
                                                      response.writeHead(statusCode, {
                                                        "Content-Type": mimeType + ';charset=utf-8',
                                                        "Cache-Control": "public, max-age=0",
                                                        "Set-Cookie": cookies,
                                                        "Last-Modified": lastModified,
                                                        "server": `node/${process.version}`,
                                                        "service": process.pid
                                                      });
                                                      resolve();
                                                    } catch (error) {
                                                      reject(error);
                                                    }
                                                  }).then(
                                                      () => {
                                                        stream.pipe(response);
                                                        callback(null, statusCode);
                                                      }
                                                      , (error) => {
                                                        stream.pipe(response);
                                                        callback(null, statusCode);
                                                      }
                                                  ).catch((error) => {
                                                    callback(error, null);
                                                  })
                                                }
                                                //not use cookie
                                                , () => {
                                                  new Promise((resolve, reject) => {
                                                    try {
                                                      response.writeHead(statusCode, {
                                                        "Content-Type": mimeType + ';charset=utf-8',
                                                        "Cache-Control": "public, max-age=0",
                                                        "Last-Modified": lastModified,
                                                        "server": `node/${process.version}`,
                                                        "service": process.pid
                                                      });
                                                      resolve();
                                                    } catch (error) {
                                                      reject(error);
                                                    }
                                                  }).then(
                                                      () => {
                                                        stream.pipe(response);
                                                        callback(null, statusCode);
                                                      }
                                                      , (error) => {
                                                        stream.pipe(response);
                                                        callback(null, statusCode);
                                                      }
                                                  ).catch((error) => {
                                                    callback(error, null);
                                                  })
                                                }
                                            ).catch((error) => {
                                              callback(error, null);
                                            })
                                          }
                                      ).catch((error) => {
                                        callback(error, null);
                                      })
                                    }
                                ).catch((error) => {
                                  callback(error, null);
                                })
                              }
                              //encoding not support
                              , () => {
                                new Promise((resolve, reject) => {
                                  cookies ? resolve() : reject();
                                }).then(
                                    //use cookie
                                    () => {
                                      new Promise((resolve, reject) => {
                                        try {
                                          response.writeHead(statusCode, {
                                            "Content-Type": mimeType + ';charset=utf-8',
                                            "Cache-Control": "public, max-age=0",
                                            "Set-Cookie": cookies,
                                            "Last-Modified": lastModified,
                                            "server": `node/${process.version}`,
                                            "service": process.pid
                                          });
                                          resolve();
                                        } catch (error) {
                                          reject(error);
                                        }
                                      }).then(
                                          () => {
                                            stream.pipe(response);
                                            callback(null, statusCode);
                                          }
                                          , (error) => {
                                            stream.pipe(response);
                                            callback(null, statusCode);
                                          }
                                      ).catch((error) => {
                                        callback(error, null);
                                      })
                                    }
                                    //not use cookie
                                    , () => {
                                      new Promise((resolve, reject) => {
                                        try {
                                          response.writeHead(statusCode, {
                                            "Content-Type": mimeType + ';charset=utf-8',
                                            "Cache-Control": "public, max-age=0",
                                            "Last-Modified": lastModified,
                                            "server": `node/${process.version}`,
                                            "service": process.pid
                                          });
                                          resolve();
                                        } catch (error) {
                                          reject(error);
                                        }
                                      }).then(
                                          () => {
                                            stream.pipe(response);
                                            callback(null, statusCode);
                                          }
                                          , (error) => {
                                            stream.pipe(response);
                                            callback(null, statusCode);
                                          }
                                      ).catch((error) => {
                                        callback(error, null);
                                      })
                                    }
                                ).catch((error) => {
                                  callback(error, null);
                                })
                              }
                          ).catch((error) => {
                            callback(error, null);
                          })
                        }
                        //have no LastModified
                        , () => {
                          callback(new SyntaxError(`LastModified can't be undefined`), null);
                        }
                    ).catch((error) => {
                      callback(error, null);
                    })
                  }
                  //not use cache
                  , () => {
                    new Promise((resolve, reject) => {
                      encoding ? resolve() : reject();
                    }).then(
                        //encoding support
                        () => {
                          new Promise((resolve, reject) => {
                            encoding.match(/gzip/) ? resolve() : reject();
                          }).then(
                              //gzip support
                              () => {
                                let compress = zlib.createGzip();
                                let compressType = 'gzip';
                                new Promise((resolve, reject) => {
                                  cookies ? resolve() : reject();
                                }).then(
                                    //use cookie
                                    () => {
                                      new Promise((resolve, reject) => {
                                        try {
                                          response.writeHead(statusCode, {
                                            "Content-Type": mimeType + ';charset=utf-8',
                                            "Cache-Control": "public, max-age=0",
                                            "Set-Cookie": cookies,
                                            'Content-Encoding': compressType,
                                            "server": `node/${process.version}`,
                                            "service": process.pid
                                          });
                                          resolve();
                                        } catch (error) {
                                          reject(error);
                                        }
                                      }).then(
                                          () => {
                                            stream.pipe(compress).pipe(response);
                                            callback(null, statusCode);
                                          }
                                          , (error) => {
                                            stream.pipe(response);
                                            callback(null, statusCode);
                                          }
                                      ).catch((error) => {
                                        callback(error, null);
                                      })
                                    }
                                    //not use cookie
                                    , () => {
                                      new Promise((resolve, reject) => {
                                        try {
                                          response.writeHead(statusCode, {
                                            "Content-Type": mimeType + ';charset=utf-8',
                                            "Cache-Control": "public, max-age=0",
                                            'Content-Encoding': compressType,
                                            "server": `node/${process.version}`,
                                            "service": process.pid
                                          });
                                          resolve();
                                        } catch (error) {
                                          reject(error);
                                        }
                                      }).then(
                                          () => {
                                            stream.pipe(compress).pipe(response);
                                            callback(null, statusCode);
                                          }
                                          , (error) => {
                                            stream.pipe(response);
                                            callback(null, statusCode);
                                          }
                                      ).catch((error) => {
                                        callback(error, null);
                                      })
                                    }
                                ).catch((error) => {
                                  callback(error, null);
                                })
                              }
                              //gzip not support
                              , () => {
                                new Promise((resolve, reject) => {
                                  encoding.match(/deflate/) ? resolve() : reject();
                                }).then(
                                    //deflate support
                                    () => {
                                      let compress = zlib.createDeflate();
                                      let compressType = 'deflate';
                                      new Promise((resolve, reject) => {
                                        cookies ? resolve() : reject();
                                      }).then(
                                          //use cookie
                                          () => {
                                            new Promise((resolve, reject) => {
                                              try {
                                                response.writeHead(statusCode, {
                                                  "Content-Type": mimeType + ';charset=utf-8',
                                                  "Cache-Control": "public, max-age=0",
                                                  "Set-Cookie": cookies,
                                                  'Content-Encoding': compressType,
                                                  "server": `node/${process.version}`,
                                                  "service": process.pid
                                                });
                                                resolve();
                                              } catch (error) {
                                                reject(error);
                                              }
                                            }).then(
                                                () => {
                                                  stream.pipe(compress).pipe(response);
                                                  callback(null, statusCode);
                                                }
                                                , (error) => {
                                                  stream.pipe(response);
                                                  callback(null, statusCode);
                                                }
                                            ).catch((error) => {
                                              callback(error, null);
                                            })
                                          }
                                          //not use cookie
                                          , () => {
                                            new Promise((resolve, reject) => {
                                              try {
                                                response.writeHead(statusCode, {
                                                  "Content-Type": mimeType + ';charset=utf-8',
                                                  "Cache-Control": "public, max-age=0",
                                                  'Content-Encoding': compressType,
                                                  "server": `node/${process.version}`,
                                                  "service": process.pid
                                                });
                                                resolve();
                                              } catch (error) {
                                                reject(error);
                                              }
                                            }).then(
                                                () => {
                                                  stream.pipe(compress).pipe(response);
                                                  callback(null, statusCode);
                                                }
                                                , (error) => {
                                                  stream.pipe(response);
                                                  callback(null, statusCode);
                                                }
                                            ).catch((error) => {
                                              callback(error, null);
                                            })
                                          }
                                      ).catch((error) => {
                                        callback(error, null);
                                      })
                                    }
                                    //encoding not support
                                    , () => {
                                      new Promise((resolve, reject) => {
                                        cookies ? resolve() : reject();
                                      }).then(
                                          //use cookie
                                          () => {
                                            new Promise((resolve, reject) => {
                                              try {
                                                response.writeHead(statusCode, {
                                                  "Content-Type": mimeType + ';charset=utf-8',
                                                  "Cache-Control": "public, max-age=0",
                                                  "Set-Cookie": cookies,
                                                  "server": `node/${process.version}`,
                                                  "service": process.pid
                                                });
                                                resolve();
                                              } catch (error) {
                                                reject(error);
                                              }
                                            }).then(
                                                () => {
                                                  stream.pipe(response);
                                                  callback(null, statusCode);
                                                }
                                                , (error) => {
                                                  stream.pipe(response);
                                                  callback(null, statusCode);
                                                }
                                            ).catch((error) => {
                                              callback(error, null);
                                            })
                                          }
                                          //not use cookie
                                          , () => {
                                            new Promise((resolve, reject) => {
                                              try {
                                                response.writeHead(statusCode, {
                                                  "Content-Type": mimeType + ';charset=utf-8',
                                                  "Cache-Control": "public, max-age=0",
                                                  "server": `node/${process.version}`,
                                                  "service": process.pid
                                                });
                                                resolve();
                                              } catch (error) {
                                                reject(error);
                                              }
                                            }).then(
                                                () => {
                                                  stream.pipe(response);
                                                  callback(null, statusCode);
                                                }
                                                , (error) => {
                                                  stream.pipe(response);
                                                  callback(null, statusCode);
                                                }
                                            ).catch((error) => {
                                              callback(error, null);
                                            })
                                          }
                                      ).catch((error) => {
                                        callback(error, null);
                                      })
                                    }
                                ).catch((error) => {
                                  callback(error, null);
                                })
                              }
                          ).catch((error) => {
                            callback(error, null);
                          })
                        }
                        //encoding not support
                        , () => {
                          new Promise((resolve, reject) => {
                            cookies ? resolve() : reject();
                          }).then(
                              //use cookie
                              () => {
                                new Promise((resolve, reject) => {
                                  try {
                                    response.writeHead(statusCode, {
                                      "Content-Type": mimeType + ';charset=utf-8',
                                      "Cache-Control": "public, max-age=0",
                                      "Set-Cookie": cookies,
                                      "server": `node/${process.version}`,
                                      "service": process.pid
                                    });
                                    resolve();
                                  } catch (error) {
                                    reject(error);
                                  }
                                }).then(
                                    () => {
                                      stream.pipe(response);
                                      callback(null, statusCode);
                                    }
                                    , (error) => {
                                      stream.pipe(response);
                                      callback(null, statusCode);
                                    }
                                ).catch((error) => {
                                  callback(error, null);
                                })
                              }
                              //not use cookie
                              , () => {
                                new Promise((resolve, reject) => {
                                  try {
                                    response.writeHead(statusCode, {
                                      "Content-Type": mimeType + ';charset=utf-8',
                                      "Cache-Control": "public, max-age=0",
                                      "server": `node/${process.version}`,
                                      "service": process.pid
                                    });
                                    resolve();
                                  } catch (error) {
                                    reject(error);
                                  }
                                }).then(
                                    () => {
                                      stream.pipe(response);
                                      callback(null, statusCode);
                                    }
                                    , (error) => {
                                      stream.pipe(response);
                                      callback(null, statusCode);
                                    }
                                ).catch((error) => {
                                  callback(error, null);
                                })
                              }
                          ).catch((error) => {
                            callback(error, null);
                          })
                        }
                    ).catch((error) => {
                      callback(error, null);
                    })
                  }
              ).catch((error) => {
                callback(error, null);
              })
            }
        ).catch((error) => {
          callback(error, null);
        })
      }
      , () => {
        callback(new SyntaxError(`response can't be undefined`));
      }
  ).catch((error) => {
    callback(error, null);
  })
}

module.exports = resWrite